package com.sc.test.tcpDemo;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @version 1.0
 * @Author: 许啸林 InetAddress 用于获取主机域名/ip地址
 * @Date: 2021/6/6 14:01
 */
public class InetAddressDemo {
    public static void main(String[] args) throws UnknownHostException {
        InetAddress localHost =  InetAddress.getLocalHost();
        System.out.println(localHost);

        // 通过 计算机名/域名 获取地址
        InetAddress getlocalByName = InetAddress.getByName("DESKTOP-KUBCTBJ");
        System.out.println("local" + getlocalByName);

        InetAddress baiduAddr = InetAddress.getByName("www.baidu.com");
        System.out.println("baidu =" + baiduAddr);

        // 通过InetAddress 获取主机ip地址
        String baiDuHostAddress = baiduAddr.getHostAddress();
        System.out.println("百度主机ip地址:" + baiDuHostAddress);

        // 通过InetAddress 获取主机域名
        String baiduHostName = baiduAddr.getHostName();
        System.out.println("百度域名: " + baiduHostName);

        //
        String str = "36.152.44.96";
        String[] ipStr = str.split("\\.");
        byte[] ipBuf = new byte[4];
        for(int i = 0; i < 4; i++){
            ipBuf[i] = (byte)(Integer.parseInt(ipStr[i])&0xff);
        }

        InetAddress ia = InetAddress.getByAddress("www.aaa",ipBuf);
        System.out.println(ia);

    }
}
